import { query,add,remove,edit ,queryResources,queryLogs,queryOneUsergroup } from '@/services/custom_menu';
import moment from 'moment';
let UUID = require('uuid-js');
export default {
  namespace: 'custom_menu',
  state: {
    pageLoaded:false,
    tableY:300,
    data:[],
    button:[],
    bottomActive:'',
    subActive:'',
    clean:true,
    scrollData:{x:0,y:0},
    selectedRowKeys: [0],
    searchParams:{
      nowDate:moment().format("YYYY-MM-DD")
    },
    meta: {total: 0, per_page: 50,current_page:1},
    additional: {total_count: 0, online_count: 0},
    loading: true,
    editRecord:{},
    detail:{
      pageLoaded:false,
      isDetail:false,
    }

  },
  effects: {
    *changeBottomActive({ payload, callback }, { call, put ,select}) {
      console.log('changeBottomActive',payload)
      yield put({
        type: 'changeBottomActiveR',
        payload:{
          bottomActive:payload
        }
      });
    },
    *changeSubActive({ payload, callback }, { call, put ,select}) {
      yield put({
        type: 'changeSubActiveR',
        payload:{
          subActive:payload
        }
      });
    },
    *changeButton({ payload, callback }, { call, put ,select}) {
      yield put({
        type: 'changeButtonR',
        payload:{
          button:payload
        }
      });
    },
    *changeIsDetail({ payload, callback }, { call, put ,select}) {
      console.log('changeIsDetail')
      const {detail} = yield select(state => state.custom_menu)
      yield put({
        type: 'changeDetail',
        payload:{
          ...detail,
          isDetail:!detail.isDetail,
          detailId:payload.id,
        }
      });
    },
    *reset({ payload, callback }, { call, put }) {
      console.log('reset=============')
      yield put({
        type: 'resetData',
        payload:payload
      });
      if(callback) callback()
    },
    *changeTableY({ payload, callback }, { call, put }) {
      const tableY=document.querySelector('.table-pagination').getBoundingClientRect().top -
        document.querySelector('.react-grid-Container').getBoundingClientRect().top -10;
      yield put({
        type: 'saveTableY',
        payload: tableY
      });
    },
    *changeSearchParams({ payload, callback }, { call, put }) {
      console.log('changeSearchParams')
      yield put({
        type: 'saveSearchParams',
        payload: payload
      });
    },
    *changeLoaded({ payload, callback }, { call, put }) {
      yield put({
        type: 'changePageLoaded',
        payload: true,
      });
    },
    *changeScroll({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeScrollData',
        payload: payload,
      });
    },
    *selectRow({payload, callback}, {call, put,select}) {
      const {selectedRowKeys} = yield select(state => state.custom_menu)
      if (selectedRowKeys.indexOf(payload.id) >= 0) {
        selectedRowKeys.splice(selectedRowKeys.indexOf(payload.id), 1);
      } else {
        selectedRowKeys.push(payload.id);
      }
      yield put({
        type: 'saveSelectRow',
        payload: selectedRowKeys,
      });
    },
    *fetch({ payload,callback }, { call, put ,select}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const state = yield select(state => state.custom_menu)//获取state中的数据
      payload=payload||state.searchParams;
      console.log('payload',payload)
      const response = yield call(query, payload);
      console.log(response)
      if(response.data.status_code===200){
        yield put({
          type: 'save',
          payload:  response.data
        });
        yield put({
          type: 'saveSearchParams',
          payload: payload
        });

        yield put({
          type: 'changeLoading',
          payload: false,
        });
        if (callback) callback();
      }


    },
    *edit({ payload, callback }, { call, put }) {
      console.log('edit')
      const response = yield call(edit, payload);
      console.log(response)
      if(response.status===200){
        if (callback) callback();
        yield put({
          type: 'changeClean',
          payload: false,
        });
      }
    },
    *changeSelectRow({payload, callback}, {call, put,select}) {
      yield put({
        type: 'saveSelectRow',
        payload: payload,
      });
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(remove, payload);
      if(response.status===200){
        if (callback) callback();
      }
    },
  },

  reducers: {

    save(state, action) {
      let button=action.payload.data.menu.button;
      let bottomActive=''
      for(let i=0;i<button.length;i++){
        button[i].uuid=UUID.create().toString();
        if(i===0){
          bottomActive=button[i].uuid
        }
        if(button[i].sub_button){
          for(let j=0;j<button[i].sub_button.length;j++){
            button[i].sub_button[j].uuid=UUID.create().toString();
          }
        }
      }
      return {
        ...state,
        button: button,
        bottomActive:bottomActive
      };
    },
    changeClean(state, action){
      return {
        ...state,
        clean:true,
      };
    },
    changeBottomActiveR(state, action){
      return {
        ...state,
        bottomActive: action.payload.bottomActive,
      };
    },
    changeSubActiveR(state, action){
      return {
        ...state,
        subActive: action.payload.subActive,
      };
    },
    changeButtonR(state, action){
      return {
        ...state,
        button: action.payload.button,
        clean:false,
      };
    },
    saveTableY(state, action) {
      return {
        ...state,
        tableY: action.payload,
      };
    },
    saveOneRecord(state, action) {
      return {
        ...state,
        editRecord:action.payload
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    changePageLoaded(state, action) {
      return {
        ...state,
        pageLoaded: action.payload,
      };
    },
    changeScrollData(state, action) {
      return {
        ...state,
        scrollData: action.payload,
      };
    },
    saveSearchParams(state, action) {
      return {
        ...state,
        searchParams: {
          ...state.searchParams,
          ...action.payload
        },
      };
    },
    resetData(state, action) {
      return  {
        pageLoaded:action.payload,
        tableY:state.tableY,
        data:[],
        button:[],
        bottomActive:'',
        subActive:'',
        clean:true,
        scrollData:{x:0,y:0},
        selectedRowKeys: [],
        searchParams:{
          page: 1,
          per_page: 50,
        },
        meta: {total: 0, per_page: 50,current_page:1},
        loading: true,
        editRecord:{},
        additional: {total_count: 0, online_count: 0},
        detail:{
          pageLoaded:false,
          isDetail:false,
        }
      };
    },
    saveSelectRow(state, action) {
      return {
        ...state,
        selectedRowKeys: action.payload,
      };
    },
  },

};
